package com.cloudspace.jindun.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.cloudspace.jindun.JindunApp;
import com.cloudspace.jindun.R;


public final class MMAlert {


    public interface SimpleAlertListener {
        void onItemClick(int whichButton);
    }

    /**
     * 一个按钮的alert框，五个方法
     *
     * @param context
     * @param msg
     * @return
     */
    public static void showAlert(final Context context, final String msg) {
        if (!(context instanceof Activity) || context == null || StringUtil.isEmpty(msg)){
            return;
        }

        ((Activity) context).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showAlert(context, msg, null);
            }
        });
    }

    // 只弹出一次
    public static void showAlertOnce(final Context context, final String msg) {
        if (StringUtil.isEmpty(msg)) {
            return;
        }
        if (myDialog == null) {
            myDialog = showAlert(context, msg, "", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(final DialogInterface dialog, final int which) {
                    dialog.cancel();
                }
            });
        } else {
            myDialog.setMessage(msg);
            myDialog.show();
        }
    }

    public static MyDialog myDialog;

    public static MyDialog showAlert(final Context context, final String msg, final String title) {
        return showAlert(context, msg, title, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(final DialogInterface dialog, final int which) {
                dialog.cancel();
            }
        });
    }

    public static MyDialog showAlert(final Context context, final String msg, final String title, final DialogInterface.OnClickListener l) {
        return showAlert(context, msg, title, JindunApp.getInstance().getString(R.string.app_ok), true, l);
    }

    public static MyDialog showAlert(final Context context, final String msg, final String title, boolean cancel, final DialogInterface.OnClickListener l) {
        return showAlert(context, msg, title, JindunApp.getInstance().getString(R.string.app_ok), cancel, l);
    }

    public static MyDialog showAlert(final Context context, final String msg, final String title, final String yes,
                                     final DialogInterface.OnClickListener l) {
        return showAlert(context, msg, title, yes, true, l);
    }

    private static boolean isShowing = false;

    public static void setIsShowing(boolean showing){
        isShowing = showing;
    }

    public static MyDialog showAlert(final Context context, final String msg, final String title, final String yes, boolean cancel,
                                     final DialogInterface.OnClickListener l) {
        if (isShowing){
            return null;
        }

        final MyDialog.Builder builder = new MyDialog.Builder(context);
        if (!StringUtil.isEmpty(title)) {
            builder.setTitle(title);
        }
        builder.setMessage(msg);
        builder.setRightButton(yes, l);
        builder.setCancelable(cancel);
        final MyDialog alert = builder.create();
        if (alert != null) {
            alert.show();
        }
        return alert;
    }


    /**
     * 自定义View的Dialog方法
     *
     * @param context
     * @param title
     * @param view
     * @param lOk
     * @return
     */

    public static MyDialog showAlert(final Context context, final String title, final View view, final DialogInterface.OnClickListener lOk) {
        return showAlert(context, title, view, context.getString(R.string.app_ok), lOk);
    }

    public static MyDialog showAlert(final Context context, final String title, final View view, final String ok, final DialogInterface.OnClickListener lOk) {

        final MyDialog.Builder builder = new MyDialog.Builder(context);
        builder.setTitle(title);
        builder.setContentView(view);
        if (ok != null && lOk != null) {
            builder.setRightButton(ok, lOk);
        } else {
            builder.setCancelable(false);
        }
        final MyDialog alert = builder.create();
        if (alert != null) {
            alert.show();
        }
        return alert;
    }

    /**
     * 设置两个按钮的方法
     *
     * @param context
     * @param msg
     * @param title
     * @param lOk
     * @param lCancel
     * @return
     */
    public static MyDialog showAlert(final Context context, final String msg, final String title,
                                     final DialogInterface.OnClickListener lOk,
                                     final DialogInterface.OnClickListener lCancel) {

        return showAlert(context, msg, title, context.getString(R.string.app_ok), context.getString(R.string.app_cancel), lOk, lCancel);
    }

    /**
     * 只有特殊情况，需要定制左右按钮文字才用到
     *
     * @param context
     * @param msg
     * @param title
     * @param ok
     * @param cancel
     * @param lOk
     * @param lCancel
     * @return
     */
    public static MyDialog showAlert(final Context context, final String msg, final String title,
                                     final String ok, final String cancel,
                                     final DialogInterface.OnClickListener lOk,
                                     final DialogInterface.OnClickListener lCancel) {

        return showAlert(context, msg, title, null, ok, cancel, true, lOk, lCancel);
    }

    /**
     * 特殊情况，里面需要自定义View
     *
     * @param context
     * @param title
     * @param view
     * @param ok
     * @param cancel
     * @param lOk
     * @param lCancel
     * @return
     */
    public static MyDialog showAlert(final Context context, final String title, final View view,
                                     final String ok, final String cancel,
                                     final DialogInterface.OnClickListener lOk,
                                     final DialogInterface.OnClickListener lCancel) {

        return showAlert(context, null, title, view, ok, cancel, true, lOk, lCancel);
    }

    public static MyDialog showAlert(final Context context, final String msg, final String title, final View view,
                                     final String ok, final String cancel, final boolean cancelable,
                                     final DialogInterface.OnClickListener lOk,
                                     final DialogInterface.OnClickListener lCancel) {

        final MyDialog.Builder builder = new MyDialog.Builder(context);
        builder.setTitle(title);
        if (msg != null) {
            builder.setMessage(msg);
        } else if (view != null) {
            builder.setContentView(view);
        }
        builder.setRightButton(ok, lOk);
        builder.setLeftButton(cancel, lCancel);
        builder.setCancelable(cancelable);
        final MyDialog alert = builder.create();
        if (alert != null) {
            alert.show();
        }
        return alert;
    }

    public static MyDialog showBodyAlert(final Context context, String title, View view) {
        return showBodyAlert(context, title, view, false);
    }

    public static MyDialog showBodyAlert(final Context context, String title, View view, boolean canCancel) {
        final MyDialog.Builder builder = new MyDialog.Builder(context);
        builder.setTitle(title);
        if (view != null) {
            builder.setContentView(view);
        }
        builder.setLeftButton(null, null);
        builder.setRightButton(null, null);
        int width = JindunApp.getInstance().getScreenWidth() * 8 / 10;
        final MyDialog alert = builder.create(width, View.GONE);
        alert.setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                return true;
            }
        });
        alert.setCanceledOnTouchOutside(canCancel);
        alert.show();
        return alert;
    }

    /**
     * ActionSheet 多选项
     *
     * @param context
     * @param items
     * @return
     */

    public static Dialog showAlert(final Context context, final String[] items, final ActionSheet.SimpleActionSheetListener itemListener) {
        return showAlert(context, items, itemListener, null, null);
    }

    //定制title
    public static Dialog showAlert(final Context context, final String[] items, final ActionSheet.SimpleActionSheetListener itemListener,
                                   final String title) {
        return showAlert(context, items, itemListener, title, null);
    }

    //定制取消文字,默认有取消按钮，（隐藏取消按钮，传入""）
    public static Dialog showAlert(final Context context, final String[] items, final ActionSheet.SimpleActionSheetListener itemListener,
                                   final String title, String cancel) {

        ActionSheet.Builder builder = ActionSheet.createBuilder(context);

        builder.setTitle(title);

        if (cancel != null) {
            builder.setCancelButtonTitle(cancel);
        }
        builder.setOtherButtonTitles(items)
                .setListener(itemListener);
        return builder.show();
    }

    //TODO 与上面重复
    public static Dialog showAlert(final Context context, final String title, final String[] items,
                                   String exit, final ActionSheet.SimpleActionSheetListener alertDo) {
        return showAlert(context, items, alertDo, title, null);// exit,
    }


    public static Dialog showViewAlert(final Context context, View view, boolean canCancel) {
        final Dialog dlg = new Dialog(context, R.style.TransparentDialog);
        dlg.setContentView(view);
        dlg.setCancelable(canCancel);
        dlg.setCanceledOnTouchOutside(canCancel);

        Window dialogWindow = dlg.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();

        dialogWindow.setGravity(Gravity.RIGHT | Gravity.TOP);
        dialogWindow.setAttributes(lp);
        return dlg;
    }


    /**
     * @return
     */
    public static Dialog showViewAlertFromBottom(final Context context, View view) {
        final Dialog dlg = new Dialog(context, R.style.ActionSheet);
        Window w = dlg.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.x = 0;
        lp.gravity = Gravity.BOTTOM;
        dlg.onWindowAttributesChanged(lp);
        dlg.setCanceledOnTouchOutside(true);
        dlg.setContentView(view);
        return dlg;
    }

    public static Dialog showViewAlertCommon(final Context context, View view) {
        final Dialog dlg = new Dialog(context, R.style.mydialog_style);
        dlg.setCanceledOnTouchOutside(true);
        dlg.setContentView(view);
        return dlg;
    }
}
